<?php
/**
 * 文章模型
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-07-01
 */
namespace app\common\model;
class ArticleModel extends BaseModel {
	private $table_name = 'article';
	
	/**
	 * 获取文章关键词
	 * @param unknown $data
	 */
	private function getKeywords($data){
	   if(!empty($data['article_keywords'])){
	       return $data['article_keywords'];
	   }else{
	       if(empty($data['article_content'])){
	           return "";
	       }else{
	            $pa = new \pa\core\Phpanalysis();
        	    $pa->SetSource(clearContent($data['article_content']));
        	    $pa->resultType = 2;
        	    $pa->notSplitLen = 10;
        	    $pa->toLower = true;
        	    $pa->unitWord = true;
        	    $pa->differFreq = true;
        	    $pa->differMax  = true;
        	    $pa->StartAnalysis(true);
        	    return $pa->GetFinallyKeywords();
	       }
	   }
	}
	/**
	 * 获取前200个文字当描述
	 * @param unknown $val
	 */
	private function getDescription($data){
	    if(!empty($data['article_description'])){
	        return $data['article_description'];
	    }else{
	        if(empty($data['article_content'])){
	            return "";
	        }else{
	            return mb_substr(clearContent($data['article_content']),0,200,'utf-8');
	        }
	    }
	}
	/**
	 * 首页获取文章列表
	 */
	public function getArticleIndex($page){
		//获取文章信息
		$condition = array();
		$condition['is_show'] = 1;
		$condition['is_delete'] = 0;
		$field = "id,article_title,article_keywords,article_description,article_author,class_id,add_time";
		$list = $this->select_by_page($this->table_name,$condition,$field,$page,10,'is_top desc,id desc');
		foreach ($list as $k=>$v){
			$list[$k]['img_k'] = $v['id']%20+1; 
		}
		return $list;
	}
	/**
	 * 分类页获取文章列表
	 */
	public function getArticleCate($cate_id,$page){
		//获取文章信息
		$condition = array();
		$condition['is_show'] = 1;
		$condition['is_delete'] = 0;
		$condition['class_id'] = $cate_id;
		$field = "id,article_title,article_keywords,article_description,article_author,class_id,click,add_time";
		return $this->select_by_page($this->table_name,$condition,$field,$page,10,'is_top desc,id desc');
	}
	/**
	 * 置顶文章
	 */
	public function getTopArticle(){
		//获取置顶文章
		$condition = array();
		$condition['is_show'] = 1;
		$condition['is_delete'] = 0;
		$condition['is_top'] = 1;
		$field = "id,article_title,article_keywords,article_description,article_author,class_id,add_time";
		return $this->select_by_page($this->table_name,$condition,$field,0,5,'is_top desc,id desc');
	}
	/**
	 * 热门文章
	 */
	public function getHotArticle(){
		//获取置顶文章
		$condition = array();
		$condition['is_show'] = 1;
		$condition['is_delete'] = 0;
		$field = "id,article_title,article_keywords,article_description,article_author,class_id,add_time";
		return $this->select_by_page($this->table_name,$condition,$field,0,10,'click desc');
	}
	
	/**
	 * 获取文章列表
	 * @param unknown $result
	 */
	public function getArticleList($result){
		$order = "id desc";
		$list = $this->select_table_by_page($this->table_name,$result['condition'],'*',$order,$result['query']);
		return $list;
	}
	/**
	 * 获取标签文章信息
	 * @param unknown $result
	 */
	public function getTagArticleList($result){
		$order = "id desc";
		$join = [
				[config('database')['prefix'].'article_tag at','a.id=at.article_id'],
		];
		$list = $this->join_table_by_page($this->table_name,'a',$join,$result['condition'],'a.*',$order,$result['query']);
		return $list;
	}
	/**
	 * 添加文章
	 */
	public function addData($data){
		$insert_data = array();
		$insert_data['class_id'] = $data['class_id'];
		$insert_data['article_title'] = $data['article_title'];
		$insert_data['article_author'] = $data['article_author'];
		$insert_data['is_show'] = $data['is_show'];
		$insert_data['is_top'] = $data['is_top'];
		$insert_data['article_content'] = clearStyle($data['article_content']);
		$insert_data['article_keywords'] = $this->getKeywords($data);
		$insert_data['article_description'] = $this->getDescription($data);
		$insert_data['article_img'] = $data['article_img'];
		$insert_data['update_time'] = time();
		$res = $this->add_table($this->table_name,$insert_data);
		if($res !== false){
		    //保存标签信息
		    if(!empty($data['tags'])){
		        model('ArticleTag')->insert_tags($data['tags'],$res);
		    }
		    //保存文章图片路径
		    if(!empty($data['article_content'])){
		        $img_urls = getImgSrc($data['article_content']);
		        model('ArticleImg')->insert_imgs($img_urls,$res);
		    }
			model('ActionLog')->action_log("添加","添加文章成功，文章编号=".$res."，文章标题=".$data['article_title']);
			return $res;
		}else{
			return false;
		}
	}
    /**
     * 获取单个文章信息
     * @param unknown $condition
     */
	public function getArticleInfo($condition){
		return $this->find_table($this->table_name,$condition);
	}
	/**
	 * 更新文章信息
	 */
	public function updateData($data){
		$update_data = array();
		$id = $data['id'];
		//获取文章信息
		$article_info = $this->getArticleInfo(array('id'=>$id));
		$update_data['class_id'] = $data['class_id'];
		$update_data['article_title'] = $data['article_title'];
		$update_data['article_author'] = $data['article_author'];
		$update_data['is_show'] = $data['is_show'];
		$update_data['is_top'] = $data['is_top'];
		$update_data['article_content'] = clearStyle($data['article_content']);
		$update_data['article_keywords'] = $this->getKeywords($data);
		$update_data['article_description'] = $this->getDescription($data);
		if(!empty($data['article_img'])){
			$update_data['article_img'] = $data['article_img'];
		}
		$update_data['update_time'] = time();
		$res = $this->update_table($this->table_name,array('id'=>$id),$update_data);
		if($res !== false){
			//删除文章封面图片
			if(!empty($article_info['article_img'])){
				delImg($article_info['article_img']);
			}
		    //保存标签信息
		    if(!empty($data['tags'])){
		        model('ArticleTag')->update_tags($data['tags'],$id);
		    }
		    //保存文章图片路径
		    if(!empty($data['article_content'])){
		        $img_urls = getImgSrc($data['article_content']);
		        model('ArticleImg')->update_imgs($img_urls,$id);
		    }
			model('ActionLog')->action_log("添加","添加文章成功，文章编号=".$res."，文章标题=".$data['article_title']);
			return $res;
		}else{
			return false;
		}
	}
	/**
	 * 删除文章信息
	 */
	public function deleteData($condition){
		$article_info = $this->getArticleInfo($condition);
		$res = $this->update_table($this->table_name,$condition,array('is_delete'=>1));
		if($res !== false){
			model('ActionLog')->action_log("删除","删除文章成功，文章编号=".$article_info['id']."，文章标题=".$article_info['article_title']);
			return $res;
		}else{
			return false;
		}
	}
}
